//
//  HZC_BaseModel.m
//  Co8
//
//  Created by Hanzengchao on 2017/11/1.
//

#import "YYJ_BaseModel.h"

@implementation YYJ_BaseModel
-(void)setValue:(id)value forKey:(NSString *)key{
    if ([value isKindOfClass:[NSNumber class]]) {
        NSNumber *NBValue = value;
        value = [NSString stringWithFormat:@"%ld",(long)NBValue.integerValue];
    }if ([value isKindOfClass:[NSNull class]]) {
        value = @"";
    }else if (value == nil){
        value = @"";
    }
    [super setValue:value forKey:key];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    NSLog(@"value = %@  key = %@",value ,key);
}
+ (NSDictionary *)replacedKeyFromPropertyName{
    return @{@"kId":@"id",@"Description":@"description"};
}
- (NSString *)logModel {
    //初始化一个字典
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    //得到当前class的所有属性
    uint count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    //循环并用KVC得到每个属性的值
    for (int i = 0; i<count; i++) {
        objc_property_t property = properties[i];
        NSString *name = @(property_getName(property));
        id value = [self valueForKey:name]?:@"";//默认值为nil字符串
        [dictionary setObject:value forKey:name];//装载到字典里
        [dictionary setObject:NSStringFromClass([value class]) forKey:[name stringByAppendingString:@"_Class"]];
    }
    //释放
    free(properties);
    //return
    return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary];
}
- (NSDictionary *)returnDict {
    //初始化一个字典
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [self debugClass:([self class]) PropertysWithDictionary:dictionary];
    //得到当前class的所有属性
    uint count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    //循环并用KVC得到每个属性的值
    for (int i = 0; i<count; i++) {
        objc_property_t property = properties[i];
        NSString *name = @(property_getName(property));
        id value = [self valueForKey:name]?:@"";//默认值为nil字符串
//        NSLog(@"key:%@",name);
        if ([value isKindOfClass:[YYJ_BaseModel class]]) {
            value = [(YYJ_BaseModel *)value returnDict];
        }
        if ([name isEqualToString:@"kId"]) {
            name = @"id";
        }
        [dictionary setObject:value forKey:name];//装载到字典里
    }
    //释放
    free(properties);
    //return
    return dictionary;
}
- (void)debugClass:(Class)cls PropertysWithDictionary:(NSMutableDictionary *)dic
{
    if (![NSStringFromClass(cls.superclass) isEqualToString:NSStringFromClass([YYJ_BaseModel class])]) {
        uint count;
        objc_property_t *properties = class_copyPropertyList(cls.superclass, &count);
        for (int i = 0; i<count; i++) {
            objc_property_t property = properties[i];
            NSString *name = @(property_getName(property));
            id value = [self valueForKey:name]?:@"";
            if ([value isKindOfClass:[YYJ_BaseModel class]]) {
                value = [(YYJ_BaseModel *)value returnDict];
            }
            if ([name isEqualToString:@"kId"]) {
                name = @"id";
            }
            [dic setObject:value forKey:name];// 注意使用self得到value
        }
        free(properties);
        [self debugClass:(cls.superclass) PropertysWithDictionary:dic];
    }
}
@end
